package com.inhim.pjhim.dowloadfile.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.inhim.pjhim.R;
import com.inhim.pjhim.dowloadfile.adapter.provider.DownloadingProvider;
import com.inhim.pjhim.dowloadfile.download.MyBusinessInfoDid;
import com.inhim.pjhim.dowloadfile.utils.FileUtil;
import com.inhim.pjhim.utils.FileUtils;
import com.inhim.pjhim.utils.ImageLoaderUtils;
import com.inhim.pjhim.view.LoadingView;

import org.litepal.LitePal;

import java.io.File;
import java.util.List;
import java.util.Map;


/**
 * Created by renpingqing on 17/1/19.
 */
public class DownloadListDidAdapter extends
        BaseRecyclerDidViewAdapter<MyBusinessInfoDid, DownloadListDidAdapter.ViewHolder> {

    private static final String TAG = "DownloadListAdapter";
    private final Context context;
    private boolean isCheck;
    private String mType;
    private NotifyDataSetChanged mNotifyDataSetChanged;
    private Map<Integer, Boolean> mDeviceHeaderMap;
    public DownloadListDidAdapter(Context context,String type) {
        super(context);
        this.context = context;
        this.mType=type;
    }

    public void setCheck(boolean isCheck, Map<Integer, Boolean> mDeviceHeaderMap) {
        this.isCheck = isCheck;
        this.mDeviceHeaderMap = mDeviceHeaderMap;
    }
    public interface NotifyDataSetChanged {
        void notifyChanged();
        void checkOnClick(boolean isCheck,int position);
    }

    public void setNotifyDataSetChanged(NotifyDataSetChanged notifyDataSetChanged) {
        this.mNotifyDataSetChanged = notifyDataSetChanged;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(
                R.layout.item_download_info1, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.bindData(getData(position), position);

        holder.itemView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(holder.checkbox.getVisibility()==View.VISIBLE){
                    if(mNotifyDataSetChanged!=null){
                        holder.checkbox.setChecked(!holder.checkbox.isChecked());
                        mNotifyDataSetChanged.checkOnClick(holder.checkbox.isChecked(),position);
                    }
                }else{
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(position);
                    }
                }
            }
        });
    }

    private int mPosition;
    class ViewHolder extends RecyclerView.ViewHolder {

        //private final ImageView iv_icon;
        private final TextView tv_size;
        private final TextView tv_status;
        private final TextView tv_name;
        private ImageView imageview1, imageview2;
        private CheckBox checkbox;

        public ViewHolder(View view) {
            super(view);
            itemView.setClickable(true);
            tv_size = view.findViewById(R.id.tv_size);
            tv_status = view.findViewById(R.id.tv_status);
            tv_name = view.findViewById(R.id.tv_name);
            imageview1 = view.findViewById(R.id.imageview1);
            imageview2 = view.findViewById(R.id.imageview2);
            checkbox = view.findViewById(R.id.checkbox);
        }

        @SuppressWarnings("unchecked")
        public void bindData(final MyBusinessInfoDid data, final int position) {
            tv_name.setText(data.getTitle());
            //tv_size.setText(FileUtil.formatFileSize(data.getTotal()));
            try {
                String strSize=FileUtils.getAutoFileOrFilesSize(data.getFilePath());
                tv_size.setText(strSize);
            }catch (Exception e){
                e.printStackTrace();
            }
            if(data.getProgressText()!=null&&!data.getProgressText().equals("")){
                tv_status.setText(data.getProgressText());
            }else{
                tv_status.setText("未播放");
            }
            ImageLoaderUtils.setImage(data.getCover(),imageview1);
            imageview2.setImageResource(R.mipmap.icon_video);
            if(isCheck){
                checkbox.setVisibility(View.VISIBLE);
            }else{
                checkbox.setVisibility(View.GONE);
            }
            if (mDeviceHeaderMap!=null && mDeviceHeaderMap.get(position)) {
                checkbox.setChecked(true);
            } else {
                checkbox.setChecked(false);
            }
            checkbox.setOnClickListener(v-> {
                if(mNotifyDataSetChanged!=null){
                    mNotifyDataSetChanged.checkOnClick(checkbox.isChecked(),position);
                }
            });
        }
    }

    public void setDate(){
        setData(getDownloadListData());
    }

    public void deleteAllFiles(){
        //LoadingView loadingView=new LoadingView();
        //loadingView.showLoading("删除中",context);
        List<MyBusinessInfoDid> listInfo = getDownloadListData();
        for(int i=0;i<listInfo.size();i++){
                try{
                    File file = new File(listInfo.get(i).getFilePath());
                    FileUtils.deleteOrFiles(file);
                    /*FileUtils.DeleteFile(file);*/
                    /*boolean isDelete = false;
                    if(file.exists()){
                        isDelete=file.delete();
                    }*/
                    //删除数据库movie表中id为1的记录
                    listInfo.get(i).delete();
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        //loadingView.hideLoading();
        //loadingView=null;
    }
    public void deleteFiles(Map<Integer, Boolean> deleteMap){
        List<MyBusinessInfoDid> listInfo = getDownloadListData();
        for(int i=0;i<deleteMap.size();i++){
            if (deleteMap.get(i)) {
                try{
                    boolean isDelete = false;
                    //FileUtils.deleteFiles(listInfo.get(i).getFilePath());
                    File file = new File(listInfo.get(i).getFilePath());
                    FileUtils.deleteOrFiles(file);
                    /*FileUtils.DeleteFile(file);*/
                    listInfo.get(i).delete();
                    /*if(file.exists()){
                        isDelete=file.delete();
                    }

                    //删除数据库movie表中id为1的记录
                    if(isDelete){
                        listInfo.get(i).delete();
                    }*/
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }
    }
    private List<MyBusinessInfoDid> getDownloadListData() {
        List<MyBusinessInfoDid> myBusinessInfos=LitePal.where("type = ?", mType).find(MyBusinessInfoDid.class);
        //List<MyBusinessInfoDid> myBusinessInfos = LitePal.findAll(MyBusinessInfoDid.class);
        if (myBusinessInfos.size() > 0) {
        }
        return myBusinessInfos;
    }

}
